package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.DeleteDTO;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.CouponTempleteDao;
import com.xbongbong.saas.domain.entity.CouponTempleteEntity;
import com.xbongbong.saas.enums.business.MarketingBaseEnum;
import com.xbongbong.saas.model.CouponTempleteModel;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

;

/**
 * @author bomfish
 */
@Service("couponTempleteModel")
public class CouponTempleteModelImpl implements CouponTempleteModel {
    @Resource
    private CouponTempleteDao couponTempleteDao;
    @Resource
    private PaasEsModel paasEsModel;

    @Override
    public Long insert(CouponTempleteEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setDel(0);
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        entity.setGetNum(BasicConstant.ZERO);
        entity.setUsedNum(BasicConstant.ZERO);
        Long success = couponTempleteDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_MARKETING_ACTIVITY_MANAGEMENT);
        paasEsModel.insert(insertDTO);
        return success;
    }

    @Override
    public void insertBatch(List<CouponTempleteEntity> list) {
        couponTempleteDao.insertBatch(list);
    }

    @Override
    public List<CouponTempleteEntity> findEntities(Map<String, Object> param) {
        return couponTempleteDao.findEntities(param);
    }

    @Override
    public Integer getEntityCount(Map<String, Object> param) {
        return couponTempleteDao.getEntitiesCount(param);
    }

    @Override
    public void update(CouponTempleteEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_MARKETING_ACTIVITY_MANAGEMENT);
        paasEsModel.update(insertDTO);
        couponTempleteDao.update(entity);
    }

    @Override
    public CouponTempleteEntity getByKey(Long key, String corpid) {
        return couponTempleteDao.getByKey(key, corpid);
    }


    @Override
    public Integer deleteByKey(Long key, String corpid) throws XbbException {
        DeleteDTO deleteDTO = new DeleteDTO();
        deleteDTO.setEsId(corpid+"_"+key);
        deleteDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_MARKETING_ACTIVITY_MANAGEMENT);
        paasEsModel.delete(deleteDTO);
        return couponTempleteDao.deleteByKey(key, corpid);
    }


    @Override
    public Integer getOnlineCouponCount(String corpid) {
        return couponTempleteDao.getOnlineCouponCount(corpid);
    }

    @Override
    public void updateUsedNum(Set<Long> idIn, List<CouponTempleteEntity> updateList, String corpid) throws XbbException {
        couponTempleteDao.updateUsedNum(updateList, corpid);

        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID,corpid);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("idIn", idIn);
        List<CouponTempleteEntity> entities = findEntities(param);
        if (entities.isEmpty()){
            return;
        }
        List<InsertDTO> updateBatchList = new ArrayList<>();
        for (CouponTempleteEntity entity : entities) {
            Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            JSONObject object = new JSONObject();
            object.put(MarketingBaseEnum.USED_NUM.getAttr(),entity.getUsedNum());
            map.put(StringConstant.JSON_DATA, object);
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertDTO.setEsId(entity.getCorpid() + "_" + entity.getId());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_MARKETING_ACTIVITY_MANAGEMENT);
            updateBatchList.add(insertDTO);
        }
        paasEsModel.updateBatch(updateBatchList, WriteRequest.RefreshPolicy.NONE);
    }

    @Override
    public void updateStatus(List<CouponTempleteEntity> updateList, String corpid) throws XbbException {
        couponTempleteDao.updateStatus(updateList, corpid);
        List<InsertDTO> updateBatchList = new ArrayList<>();
        for (CouponTempleteEntity entity : updateList) {
            Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            JSONObject object = new JSONObject();
            object.put(MarketingBaseEnum.STATUS.getAttr(),entity.getStatus());
            object.put(MarketingBaseEnum.GET_NUM.getAttr(),entity.getGetNum());
            map.put(StringConstant.JSON_DATA, object);
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertDTO.setEsId(entity.getCorpid() + "_" + entity.getId());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_MARKETING_ACTIVITY_MANAGEMENT);
            updateBatchList.add(insertDTO);
        }
        paasEsModel.updateBatch(updateBatchList, WriteRequest.RefreshPolicy.NONE, false);
    }

    @Override
    public List<String> findCorpid() {
        return couponTempleteDao.findCorpid();
    }

}
